<template>
  <div>
    <el-dialog
      :title="title"
      :closeOnClickModal="false"
      :visible.sync="show"
      width="650px">
      <el-form 
        label-position="right" 
        :model="selectTable"
        ref="addForm" 
        label-width="80px" 
        :inline="false"
        autocomplete="off"
        >
        <el-form-item label="标签名称" prop="tagsName" :rules="[rule[0]]">
          <el-input placeholder="请输入标签名称" v-model="selectTable.tagsName" maxLength="20" style="width:200px"></el-input>
        </el-form-item>
        <el-form-item label="所属分类" prop="tagsClassifyId" :rules="[rule[0]]">
          <cateselect v-model="selectTable.tagsClassifyId"></cateselect>
        </el-form-item>
        <el-form-item label="备注" prop="comment">
          <el-input placeholder="请输入备注" v-model="selectTable.comment" maxLength="50" style="width:450px"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-loading="pload">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('addForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {tagsSave,tagsModify} from '@/utils/api'
import cateselect from './cateSelect'
export default {
  components: {
    cateselect
  },
  props: {
    data: Object
  },
  created () {
    this.selectTable =  this.JSONs(this.data)
  },
  watch: {
    show (newVal) {
      this.$parent.edit = false;
    }
  },
  computed: {
    title () {
      if (this.selectTable.method==='edit') {
        return '修改';
      }
      else if (this.selectTable.method==='add') {
        return '添加';
      }
    }
  },
  data () {
    return {
      pload: false,
      rule: [
        { required: true, message: '必填项', trigger: 'change' }
      ],
      show: true,
      selectTable: {}
    }
  },
  methods: {
    async submitForm(formName) {
      await this.$refs[formName].validate();
      try {
        this.pload = true
        if (this.selectTable.method==='edit') {
          await tagsModify(this.selectTable)
        }
        else if (this.selectTable.method==='add') {
          await tagsSave(this.selectTable)
        }
        this.$notify({title: '操作成功', type: 'success', duration: 5000})
        this.$parent.edit = false;
        this.$emit('getData');
      } catch(e) {
        this.pload = false
      }
    }
  }
}
</script>